package com.ben.multiple.fdfs;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @author BenJin Yin
 * @date 2020/6/30
 */
@SpringBootTest
public class FdfsResource {
    static final String URL_PREFIX = "http://localhost:8080/file";
    static TestRestTemplate restTemplate;

    @BeforeAll
    static void init() {
        restTemplate = new TestRestTemplate(new RestTemplateBuilder());
    }

    @Test
    void upload() {
        String url = URL_PREFIX + "/upload";
        String path = "E:\\测试用图\\46382224.jpeg";
        File file = new File(path);
        if (!file.exists() || !file.isFile()) {
            return;
        }
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("file", fileSystemResource);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map, httpHeaders);
        ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        this.print(entity);
    }

    @Test
    void delete() {
        String filePath = "group1/M00/00/00/fEa0zF8gRIKAIaWiACl4g9_bxNc076.jpg";
        String param = "?filePath=" + filePath;
        String url = URL_PREFIX + "/delete" + param;
        ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.DELETE, null, String.class);
        this.print(entity);
    }

    @Test
    void download() throws IOException {
        String filePath = "group1/M00/00/00/fEa0zF8gU0mAanhFACFLYzRIDPg871.jpg";
        String param = "?filePath=" + filePath;
        String url = URL_PREFIX + "/download" + param;
        ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(null), byte[].class);
        FileOutputStream out = new FileOutputStream(new File("D:\\666.jpeg"));
        out.write(entity.getBody());
        out.close();
        System.out.println("ok");
    }

    void print(ResponseEntity<?> entity) {
        if (entity == null || HttpStatus.OK != entity.getStatusCode()) {
            System.out.println(entity);
            return;
        }
        System.out.println(entity.getBody());
    }
}
